package com.taxinfo.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.taxinfo.common.ClientSession;
import com.taxinfo.component.alert.MyAlert;
import com.taxinfo.config.ImageSrc;
import com.taxinfo.pojo.dto.*;
import com.taxinfo.pojo.vo.DecDocumentRec;
import com.taxinfo.service.*;
import com.taxinfo.utils.SpringUtil;
import com.taxinfo.view.dataImport.AccBalMapInfoTable;
import com.taxinfo.view.dataImport.AssetAndLibTable;
import com.taxinfo.view.dataImport.DataImportTable;
import com.taxinfo.view.dataImport.IncomeStmInfoTable;
import com.taxinfo.view.declaration.DecFileViewTable;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 企业申报导入
 * @author wanghy
 */
public class MainReportController extends BaseController implements Initializable {

    private final MainReportService mainReportService  = (MainReportService) SpringUtil.getBean("MainReportServiceImpl");
    private final AccBalMapInfoService accBalMapInfoService = (AccBalMapInfoService) SpringUtil.getBean("AccBalMapInfoServiceImpl");
    private final AssetAndLibService assetAndLibService = (AssetAndLibService) SpringUtil.getBean("AssetAndLibServiceImpl");
    private final IncomeStmInfoService incomeStmInfoService = (IncomeStmInfoService) SpringUtil.getBean("IncomeStmInfoServiceImpl");
    @FXML
    public ScrollPane scrollPane;

    @FXML
    public ComboBox sblx;

    @FXML
    public ComboBox sbZlx;

    @FXML
    public TextField decYear;

    private DataImportTable table;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initView();
    }

    private void initView() {
        List<MainReportDto> dtoList = mainReportService.selectAllData();
        table = new DataImportTable(dtoList);
        scrollPane.setContent(table);
        ContextMenu cxmenu = new ContextMenu();
        MenuItem accBalMapMenu = new MenuItem("查看余额表");
        MenuItem balMatchMenu = new MenuItem("导出余额表");
        MenuItem assetAndLibMenu = new MenuItem("查看资产负债");
        MenuItem assetMatchMenu = new MenuItem("导出资产负债");
        MenuItem incomeMenu = new MenuItem("查看利润表");
        MenuItem incomeMatchMenu = new MenuItem("导出利润表");
        MenuItem sbfilesMenu = new MenuItem("查看申报记录");
        MenuItem createSbFile = new MenuItem("生成税务申报表");
        sbZlx.getItems().clear();
        sblx.getItems().clear();
        sbZlx.setStyle("-fx-font-size: 14");
        sblx.setStyle("-fx-font-size: 14");
        sblx.getItems().add("全部");
        sblx.getItems().add("年度");
        sblx.getItems().add("季度");
        sblx.getItems().add("月度");
        sblx.setValue("全部");
        sblx.valueProperty().addListener((observable, oldValue, newValue) -> {
             if("季度".equals(newValue)){
                sbZlx.getItems().clear();
                sbZlx.setValue("");
                sbZlx.getItems().add("1季度");
                sbZlx.getItems().add("2季度");
                sbZlx.getItems().add("3季度");
                sbZlx.getItems().add("4季度");
            }else if("月度".equals(newValue)){
                 sbZlx.getItems().clear();
                 sbZlx.setValue("");
                 sbZlx.getItems().add("1月");
                 sbZlx.getItems().add("2月");
                 sbZlx.getItems().add("3月");
                 sbZlx.getItems().add("4月");
                 sbZlx.getItems().add("5月");
                 sbZlx.getItems().add("6月");
                 sbZlx.getItems().add("7月");
                 sbZlx.getItems().add("8月");
                 sbZlx.getItems().add("9月");
                 sbZlx.getItems().add("10月");
                 sbZlx.getItems().add("11月");
                 sbZlx.getItems().add("12月");
             }else if("年度".equals(newValue)){
                 sbZlx.getItems().clear();
                 sbZlx.setValue(decYear.getText());
                 sbZlx.getItems().add(decYear.getText());
             }else{
                 sbZlx.getItems().clear();
                 sbZlx.setValue("");
             }
        });
        //查看余额表匹配记录
        accBalMapMenu.setOnAction(event -> {
            String id = table.getSelectedData().getId();
            Dialog<String> dialog = new Dialog<>();
            dialog.setTitle("余额表匹配记录");
            dialog.setResizable(true);
            DialogPane pane = dialog.getDialogPane();
            Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
            stage.getIcons().add(new Image(ImageSrc.lOGO_PATH));
            pane.getButtonTypes().add(ButtonType.OK);
            ScrollPane scrollPane = new ScrollPane();
            scrollPane.setFitToWidth(true);
            scrollPane.setFitToHeight(true);
            //scrollPane.setFitToHeight(true);
            //scrollPane.setFitToWidth(true);
            scrollPane.setContent(new AccBalMapInfoTable(id));
            pane.setContent(scrollPane);
            dialog.showAndWait();
        });

        //查看资产负债表记录
        assetAndLibMenu.setOnAction(event -> {
            String id = table.getSelectedData().getId();
            Dialog<String> dialog = new Dialog<>();
            dialog.setTitle("资产负债匹配记录");
            dialog.setResizable(true);
            scrollPane.setFitToWidth(true);
            scrollPane.setFitToHeight(true);
            DialogPane pane = dialog.getDialogPane();
            Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
            stage.getIcons().add(new Image(ImageSrc.lOGO_PATH));
            pane.getButtonTypes().add(ButtonType.OK);
            ScrollPane scrollPane = new ScrollPane();
            //scrollPane.setFitToHeight(true);
            //scrollPane.setFitToWidth(true);
            scrollPane.setContent(new AssetAndLibTable(id));
            pane.setContent(scrollPane);
            dialog.showAndWait();
        });

        //利润表记录
        incomeMenu.setOnAction(event -> {
            String id = table.getSelectedData().getId();
            Dialog<String> dialog = new Dialog<>();
            dialog.setTitle("利润表匹配记录");
            dialog.setResizable(true);
            scrollPane.setFitToWidth(true);
            scrollPane.setFitToHeight(true);
            DialogPane pane = dialog.getDialogPane();
            Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
            stage.getIcons().add(new Image(ImageSrc.lOGO_PATH));
            pane.getButtonTypes().add(ButtonType.OK);
            ScrollPane scrollPane = new ScrollPane();
            //scrollPane.setFitToHeight(true);
            //scrollPane.setFitToWidth(true);
            scrollPane.setContent(new IncomeStmInfoTable(id));
            pane.setContent(scrollPane);
            dialog.showAndWait();
        });

        balMatchMenu.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            // 设置初始目录，例如用户的文档目录
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + "/Documents"));
            // 设置默认文件名和扩展名，例如 "document.txt"
            String fileName = table.getSelectedData().getDecYear().concat("科目余额表映射记录");
            fileChooser.setInitialFileName(fileName);
            // 设置文件过滤器，例如只显示文本文件
            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("xlsx files (*.xlsx)", "*.xlsx");
            fileChooser.getExtensionFilters().add(extFilter);

            String id = table.getSelectedData().getId();
            List<AccBalMapInfoDto>  accBalMapInfos = accBalMapInfoService.selectAllData(id);
            // 显示另存为对话框
            File selectedFile = fileChooser.showSaveDialog(null);
            String targetFilePath = "";
            if (selectedFile != null) {
                targetFilePath = selectedFile.getPath();
            }else {
                targetFilePath =  new File(System.getProperty("user.home") + "/Documents").getPath();
            }
            if(null == selectedFile){
                return;
            }
            String msg = mainReportService.exportSubMapInfoFile(targetFilePath,fileName,accBalMapInfos);
            if(StringUtils.isNotBlank(msg)){
                new MyAlert(Alert.AlertType.ERROR, msg).show();
            }else{
                new MyAlert(Alert.AlertType.INFORMATION, "科目余额表映射记录导出成功.").show();
            }
        });

        assetMatchMenu.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            // 设置初始目录，例如用户的文档目录
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + "/Documents"));
            // 设置默认文件名和扩展名，例如 "document.txt"
            String fileName = table.getSelectedData().getDecYear().concat("资产负债表映射记录");
            fileChooser.setInitialFileName(fileName);
            // 设置文件过滤器，例如只显示文本文件
            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("xlsx files (*.xlsx)", "*.xlsx");
            fileChooser.getExtensionFilters().add(extFilter);

            String id = table.getSelectedData().getId();
            List<AssetAndLibDto>  assetAndLibDtos = assetAndLibService.selectAllData(id);
            // 显示另存为对话框
            File selectedFile = fileChooser.showSaveDialog(null);
            String targetFilePath = "";
            if (selectedFile != null) {
                targetFilePath = selectedFile.getPath();
            }else {
                targetFilePath =  new File(System.getProperty("user.home") + "/Documents").getPath();
            }
            if(null == selectedFile){
                return;
            }
            String msg = mainReportService.exportAssetFile(targetFilePath,fileName,assetAndLibDtos);
            if(StringUtils.isNotBlank(msg)){
                new MyAlert(Alert.AlertType.ERROR, msg).show();
            }else{
                new MyAlert(Alert.AlertType.INFORMATION, "资产负债表映射记录导出成功.").show();
            }
        });

        incomeMatchMenu.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            // 设置初始目录，例如用户的文档目录
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home") + "/Documents"));
            // 设置默认文件名和扩展名，例如 "document.txt"
            String fileName = table.getSelectedData().getDecYear().concat("利润表映射记录");
            fileChooser.setInitialFileName(fileName);
            // 设置文件过滤器，例如只显示文本文件
            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("xlsx files (*.xlsx)", "*.xlsx");
            fileChooser.getExtensionFilters().add(extFilter);

            String id = table.getSelectedData().getId();
            List<IncomeStmInfoDto>  incomeStmInfos = incomeStmInfoService.selectAllData(id);
            // 显示另存为对话框
            File selectedFile = fileChooser.showSaveDialog(null);
            String targetFilePath = "";
            if (selectedFile != null) {
                targetFilePath = selectedFile.getPath();
            }else {
                targetFilePath =  new File(System.getProperty("user.home") + "/Documents").getPath();
            }
            if(null == selectedFile){
                return;
            }
            String msg = mainReportService.exportIncFile(targetFilePath,fileName,incomeStmInfos);
            if(StringUtils.isNotBlank(msg)){
                new MyAlert(Alert.AlertType.ERROR, msg).show();
            }else{
                new MyAlert(Alert.AlertType.INFORMATION, "利润表映射记录导出成功.").show();
            }
        });

        //查看申报文件记录
        sbfilesMenu.setOnAction(event -> {
            String id = table.getSelectedData().getId();
            Dialog<String> dialog = new Dialog<>();
            dialog.setTitle("申报文件记录");
            dialog.setResizable(true);
            DialogPane pane = dialog.getDialogPane();
            Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
            stage.getIcons().add(new Image(ImageSrc.lOGO_PATH));
            pane.getButtonTypes().add(ButtonType.OK);

            ScrollPane scrollPane = new ScrollPane();
            //scrollPane.setFitToHeight(true);
            scrollPane.setFitToWidth(true);
            scrollPane.setContent(new DecFileViewTable(id));
            pane.setContent(scrollPane);
            dialog.showAndWait();

        });

        //生成税务申报表
        createSbFile.setOnAction(event -> {
            Dialog<String> dialog = new Dialog<>();
            dialog.setResizable(true);
            DialogPane pane = dialog.getDialogPane();
            pane.setPrefSize(400,200);
            Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
            stage.getIcons().add(new Image(ImageSrc.lOGO_PATH));
            GridPane gridPane = new GridPane();
            gridPane.setVgap(10);
            gridPane.setAlignment(Pos.CENTER);
            String selectId = table.getSelectedData().getId();
            String selectSblx = table.getSelectedData().getSbType();
            String selectSbYear = table.getSelectedData().getDecYear();

            // 设置按钮和事件处理（可选）
            ButtonType buttonTypeOK = new ButtonType("生成申报表");
            pane.getButtonTypes().add(buttonTypeOK);
            pane.getButtonTypes().add(ButtonType.CANCEL);
            dialog.setResultConverter(dialogButton -> dialogButton == buttonTypeOK ? "OK" : null);

            String id = table.getSelectedData().getId();
            String sblx = table.getSelectedData().getSbType();
            String sbYear = table.getSelectedData().getDecYear();
            LoginDto loginDto =(LoginDto) ClientSession.getAttribute("user");
            String taxNo = loginDto.getCreditCode();
            String taxCompany = loginDto.getCompanyName();

            if("年度".equals(selectSblx)){
                dialog.setTitle("年度申报表选择");
                CheckBox chb1  = new CheckBox();
                chb1.setDisable(true);
                gridPane.add(chb1,1,1);
                Label lchb1 = new Label("财务报表2007旧准则.xlsx");
                lchb1.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb1.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb1,2,1);

                CheckBox chb2  = new CheckBox();
                chb2.setDisable(true);
                gridPane.add(chb2,1,2);
                Label lchb2 = new Label("财务报表2013小企业会计准则.xlsx");
                lchb2.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb2.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb2,2,2);

                CheckBox chb3  = new CheckBox();
                chb3.setDisable(true);
                gridPane.add(chb3,1,3);
                Label lchb3 = new Label("财务报表2019新准则.xlsx");
                lchb3.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb3.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb3,2,3);

                CheckBox chb4  = new CheckBox();
                chb4.setDisable(true);
                gridPane.add(chb4,1,4);
                Label lchb4 = new Label("企业所得税年度纳税申报表（A类）.xlsx");
                lchb4.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb4.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb4,2,4);

                if("0".equals(loginDto.getIndexStandardType())){
                    chb1.setSelected(true);
                }else if("1".equals(loginDto.getIndexStandardType())){
                    chb3.setSelected(true);
                }else if("2".equals(loginDto.getIndexStandardType())){
                    chb2.setSelected(true);
                }
                chb4.setSelected(true);
                dialog.getDialogPane().setContent(gridPane);
                dialog.showAndWait().ifPresent(response -> {
                    if ("OK".equals(response)) {
                        String msg = "";
                        if(chb1.isSelected()){
                            String fileName = mainReportService.createDecFile(id, taxNo, taxCompany, sbYear, "年度", lchb1.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb1.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb1.getText()));
                                }

                                //new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb1.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }
                        }else if(chb2.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"年度",lchb2.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb2.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb2.getText()));
                                }
                                //new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb2.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }
                        }else if(chb3.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"年度",lchb3.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb3.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb3.getText()));
                                }
                                //new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb3.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }
                        }
                        if(chb4.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"年度",lchb4.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb4.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb4.getText()));
                                }
                                new MyAlert(Alert.AlertType.INFORMATION, msg.concat(" 已生成,请到申报记录查看。")).show();
                            }
                        }
                   }
                });

            }else if("季度".equals(selectSblx)){
                dialog.setTitle("季度申报表选择");
                CheckBox chb1  = new CheckBox();
                chb1.setDisable(true);
                gridPane.add(chb1,1,1);
                Label lchb1 = new Label("财务报表2007旧准则.xlsx");
                lchb1.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb1.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb1,2,1);

                CheckBox chb2  = new CheckBox();
                chb2.setDisable(true);
                gridPane.add(chb2,1,2);
                Label lchb2 = new Label("财务报表2013小企业会计准则.xlsx");
                lchb2.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb2.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb2,2,2);

                CheckBox chb3  = new CheckBox();
                chb3.setDisable(true);
                gridPane.add(chb3,1,3);
                Label lchb3 = new Label("财务报表2019新准则.xlsx");
                lchb3.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb3.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb3,2,3);

                CheckBox chb4  = new CheckBox();
                chb4.setDisable(true);
                gridPane.add(chb4,1,4);
                Label lchb4 = new Label("企业所得税月（季）度预缴纳税申报表（A类）.xlsx");
                lchb4.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb4.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb4,2,4);
                if("0".equals(loginDto.getIndexStandardType())){
                    chb1.setSelected(true);
                }else if("1".equals(loginDto.getIndexStandardType())){
                    chb3.setSelected(true);
                }else if("2".equals(loginDto.getIndexStandardType())){
                    chb2.setSelected(true);
                }
                chb4.setSelected(true);
                dialog.getDialogPane().setContent(gridPane);

                dialog.showAndWait().ifPresent(response -> {
                    String msg = "";
                    if ("OK".equals(response)) {
                        if(chb1.isSelected()){

                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"季度",lchb1.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb1.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb1.getText()));
                                }
                                //new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb1.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }

                        }else if(chb2.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"季度",lchb2.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb2.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb2.getText()));
                                }
                                //new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb2.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }
                        }else  if(chb3.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"季度",lchb3.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb3.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb3.getText()));
                                }
                                //new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb3.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }
                        }
                        if(chb4.isSelected()){

                            String fileName =   mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"季度",lchb4.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                if(StringUtils.isBlank(msg)){
                                    msg = sbYear.concat("-").concat(lchb3.getText());
                                }else{
                                    msg =msg.concat("&").concat(sbYear.concat("-").concat(lchb4.getText()));
                                }
                                new MyAlert(Alert.AlertType.INFORMATION, msg.concat(" 已生成,请到申报记录查看。")).show();
                            }
                        }

                    }
                });

            }else if("月度".equals(selectSblx)){
                dialog.setTitle("月度申报表选择");
                CheckBox chb1  = new CheckBox();
                chb1.setDisable(true);
                gridPane.add(chb1,1,1);
                Label lchb1 = new Label("财务报表2007旧准则.xlsx");
                lchb1.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb1.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb1,2,1);

                CheckBox chb2  = new CheckBox();
                chb2.setDisable(true);
                gridPane.add(chb2,1,2);
                Label lchb2 = new Label("财务报表2013小企业会计准则.xlsx");
                lchb2.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb2.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb2,2,2);

                CheckBox chb3  = new CheckBox();
                chb3.setDisable(true);
                gridPane.add(chb3,1,3);
                Label lchb3 = new Label("财务报表2019新准则.xlsx");
                lchb3.setStyle("-fx-font-size: 14;-fx-padding: 6px;");
                lchb3.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(lchb3,2,3);
                if("0".equals(loginDto.getIndexStandardType())){
                    chb1.setSelected(true);
                }else if("1".equals(loginDto.getIndexStandardType())){
                    chb3.setSelected(true);
                }else if("2".equals(loginDto.getIndexStandardType())){
                    chb2.setSelected(true);
                }
                dialog.getDialogPane().setContent(gridPane);
                dialog.showAndWait().ifPresent(response -> {
                    String msg = "";
                    if ("OK".equals(response)) {
                        if(chb1.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"月度",lchb1.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb1.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }

                        }else if(chb2.isSelected()){
                            String fileName =   mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"月度",lchb2.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb2.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }

                        }else if(chb3.isSelected()){
                            String fileName =  mainReportService.createDecFile(id,taxNo,taxCompany, sbYear,"月度",lchb3.getText());
                            if(StringUtils.isNotBlank(fileName)){
                                mainReportService.saveFileRecord(id,fileName);
                                new MyAlert(Alert.AlertType.INFORMATION, sbYear.concat("-").concat(lchb3.getText().concat(" 已生成,请到申报记录查看。"))).show();
                            }
                        }else{
                            dialog.setContentText("请选择一个生成申报表");
                        }
                    }
                });
            }else{
                new MyAlert(Alert.AlertType.ERROR, "申报类型为空！").show();
                return;
            }

        });

        // 将菜单项添加到ContextMenu中
//        String type  = table.getSelectedData().getSbType();
        cxmenu.getItems().addAll(createSbFile,accBalMapMenu,assetAndLibMenu,incomeMenu,balMatchMenu,assetMatchMenu,incomeMatchMenu, sbfilesMenu);
//        if("科目余额表".equals(type)){
//            cxmenu.getItems().addAll(createSbFile,, sbfilesMenu);
//        }else{

//        }

        scrollPane.setContextMenu(cxmenu);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int year = cal.get(Calendar.YEAR);
        decYear.setText(String.valueOf(year));
    }

    @FXML
    private void findInfo() {
        String pDecYear = decYear.getText() == null ? "": decYear.getText().trim();
        String pSubType = "全部".equals(sblx.getValue())  ? "" : sblx.getValue().toString();
        List<MainReportDto> dtoList = mainReportService.selectDataByCondition(pDecYear,pSubType);
        table.clearData();
        if (dtoList != null && dtoList.size()>0) {
            for (MainReportDto bean : dtoList)
                table.addBean(bean);
        } else {
            new MyAlert(Alert.AlertType.INFORMATION, "没有找到您查找的申报信息！").show();
        }
    }

    /**
     * 申报数据导入
     */
    @FXML
    private void dataImport(){
        String[] param = this.checkCondition();
        if(null != param){
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择余额表文件");
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("余额表文件", "*.xls;*.xlsx"));
            File file = fileChooser.showOpenDialog(null); // 或者使用某个特定的Stage作为参数
            if (file!= null) {
                String msg = mainReportService.impBalanceData(file,param[0],param[1]);
                if("0".equals(msg.substring(0,1))){
                    new MyAlert(Alert.AlertType.INFORMATION, msg.substring(1)).show();
                }else{
                    new MyAlert(Alert.AlertType.ERROR, "导入余额表失败，"+msg).show();
                }
                this.findInfo();
            }
        }
    }

    @FXML
    private void balMapImp(){
        String[] param = this.checkCondition();
        Map<String, List<String>> columnInfo =super.getColumnInfo("");
        if(null != param){
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择财务报表文件");
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("财务报表财务报表文件", "*.xls;*.xlsx"));
            File file = fileChooser.showOpenDialog(null); // 或者使用某个特定的Stage作为参数
            if (file != null) {
                String msg =  mainReportService.initAccXlsx(file,param[0],param[1],columnInfo);
                if(StringUtils.isNotBlank(msg) && "0".equals(msg.substring(0,1))){
                    new MyAlert(Alert.AlertType.WARNING, msg.substring(1)).show();
                }else if(StringUtils.isNotBlank(msg)){
                    new MyAlert(Alert.AlertType.ERROR, "财务报表中企业科目匹配失败，"+msg).show();
                }else{
                    new MyAlert(Alert.AlertType.INFORMATION, "财务报表中企业科目自动匹配完成，请核对匹配结果!").show();
                }
                this.findInfo();
            }
        }
    }

    private String[] checkCondition(){
        String pDecYear = decYear.getText() == null ? "": decYear.getText().trim();
        //1:年度,2:季度,3:月度',
        String[] rtnVal = new String[2];
        String pSubType = "全部".equals(sblx.getValue())  ? "" : sblx.getValue().toString();
        rtnVal[1] = pSubType;
        if("".equals(pDecYear) || "".equals(pSubType)){
            new MyAlert(Alert.AlertType.INFORMATION, "请选择申报类型和年季月！").show();
            return null;
        }
        String sbZlx = this.sbZlx == null ? "": this.sbZlx.getValue().toString();
        if(!"".equals(pSubType) && !"年度".equals(pSubType) && "".equals(sbZlx)){
            new MyAlert(Alert.AlertType.INFORMATION, "请选择对应的季度或月份！").show();
            return null;
        }
        String sDecYear = "";
       if("年度".equals(pSubType)){
            sDecYear = pDecYear.concat("年");
        }else if("季度".equals(pSubType)){
            sDecYear = pDecYear.concat("年").concat(sbZlx);
        }else{
            sDecYear = pDecYear.concat("年").concat(sbZlx);
        }
        rtnVal[0] = sDecYear;

       return rtnVal;
    }
}
